<?php
include_once('connection/connection.php');


if(isset($_POST['submit']))
{
		//$q2_id = $_SESSION['q2_id'];
		//echo $q2_id;
		$id=$_REQUEST['Id'];
		$q2_id=$_REQUEST['frmId'];
		list($habit_code,$habit_name) = explode(",",$_POST['habitation_name']);
		
		$hname=$habit_name;
		$hcode=$habit_code;
		$type_mh_oh=$_POST['type_mh_oh'];
		$location=$_POST['location'];
		$latitude=$_POST['latitude'];
		$longitude=$_POST['longitude'];
		$function_status=$_POST['function_status'];
			
			if($function_status=="NOT FUNCTIONAL"){
			$_POST['opration_status']=$_POST['quality_status']=$_POST['yield']=$_POST['seasonal_flunctuation']=$_POST['water_table_fluctuation_min']=$_POST['water_table_fluctuation_max']=$_POST['para_ph']=$_POST['para_tds']=$_POST['para_cl']=$_POST['para_f']=$_POST['para_no3']=$_POST['firstinput']="";
			}
		$opration_status=$_POST['opration_status'];
		$quality_status=$_POST['quality_status'];
		$yield=$_POST['yield'];
		$seasonal_flunctuation=$_POST['seasonal_flunctuation'];
		$water_table_fluctuation_min=$_POST['water_table_fluctuation_min'];
		$water_table_fluctuation_max=$_POST['water_table_fluctuation_max'];
		$para_ph=$_POST['para_ph'];
		$para_tds=$_POST['para_tds'];
		$para_cl=$_POST['para_cl'];
		$para_f=$_POST['para_f'];
		$para_no3=$_POST['para_no3'];
		$para_testing_date=$_POST['firstinput'];
		$repairable_status=$_POST['Repairable'];
		$remarks=$_POST['remarks'];
		$hp_sp =$_POST['hp_sp'];	
			
			
			$qr="UPDATE tbl_dtls_q2_hp_status set habitation_name='$hname', habit_code = '$habit_code' ,type_mh_oh='$type_mh_oh', hp_sp='$hp_sp' , location='$location', latitude='$latitude', longitude='$longitude' ,functional_status='$function_status', operational_status='$opration_status', quality_status='$quality_status', yield='$yield', seasonal_fluctuation='$seasonal_flunctuation', water_table_min='$water_table_fluctuation_min', water_table_max='$water_table_fluctuation_max', para_ph='$_POST[para_ph]' , para_tds='$_POST[para_tds]' , para_cl='$_POST[para_cl]', para_f='$_POST[para_f]' , para_no3='$_POST[para_no3]' , para_testing_date='$para_testing_date',last_modify=NOW(),repairable_status='$repairable_status', remark='$remarks' where q2_id='$q2_id' and id='$id'";//exit; 
			
			$res=mysql_query($qr);
			//echo $res;
			
			$qr1="INSERT INTO tbl_dtls_q2_hp_status_temp(q2_id, habit_code , habitation_name, type_mh_oh, hp_sp , location, latitude, longitude, functional_status, operational_status, quality_status, yield, seasonal_fluctuation, water_table_min, water_table_max, para_ph, para_tds, para_cl, para_f, para_no3,para_testing_date,last_modify, repairable_status, remark) VALUES ('$q2_id', '$habit_code' , '$hname', '$type_mh_oh', '$hp_sp' ,'$location', '$latitude' , '$longitude' ,'$function_status', '$opration_status', '$quality_status', '$yield', '$seasonal_flunctuation', '$water_table_fluctuation_min', '$water_table_fluctuation_max','$para_ph','$para_tds','$para_cl','$para_f','$para_no3','$para_testing_date', NOW(),'$repairable_status', '$remarks')";
			
			$res1=mysql_query($qr1);
				
			function countSource($hcode,$hp_sp)
			{
				$qryCount="select count(*) as no from tbl_dtls_q2_hp_status where habit_code='$hcode' and hp_sp='$hp_sp'";
				$recCount=mysql_query($qryCount);
				$rowCount=mysql_fetch_array($recCount);
				return $rowCount[0];
			}
			$qryHabit="select * from tbl_dtls_q2_habit where q2_id='$q2_id' group by habit_code";
			$recHabit=mysql_query($qryHabit);
			while($rowHabit=mysql_fetch_array($recHabit))
			{
				$totalHP=countSource("$rowHabit[habit_code]","HP");
				$totalSP=countSource("$rowHabit[habit_code]","SP");
			
				$a="update tbl_dtls_q2_habit set hp_no='$totalHP',sp_no='$totalSP' where habit_code='$rowHabit[habit_code]'";
				mysql_query($a);
			}
			
			$qryHistory="insert into tbl_new_user_history(user_id,form_id,action_id,ip_address,modify_date,user_type,user_name) values('$_POST[loginId]','$q2_id','108','$_SERVER[REMOTE_ADDR]',NOW(),'$_POST[userType]','$_POST[name]')";
//mysql_query($qryHistory);
	
			if($res && $res1)
			{
				header('Location: q2.php');
				
			}
			
		}		
		
?>
